package com.itheima.admin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.Service.AdchannelService;
import com.itheima.admin.mapper.AdchannelMapper;
import com.itheima.model.common.admin.dtos.ChannelDto;
import com.itheima.model.common.admin.pojos.AdChannel;
import com.itheima.model.common.dtos.PageResponseResult;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdchannelMapper, AdChannel> implements AdchannelService {

    @Override
    public ResponseResult findbyNameAndPage(ChannelDto channelDto) {
        if (channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        channelDto.checkParam();
        Page page = new Page(channelDto.getPage(), channelDto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判读频道是否为空
        if (StringUtils.isNoneBlank(channelDto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,channelDto.getName());

        }
        IPage result = page(page, lambdaQueryWrapper);

        //返回结果
        PageResponseResult responseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());


        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        if (adChannel==null){
            return ResponseResult.errorResult((AppHttpCodeEnum.PARAM_INVALID));
        }
        if(adChannel.getDescription().length()>5000||adChannel.getName().length()>100){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOO_LONG);
        }
        List<AdChannel> adChannels = this.findAll();
        for (AdChannel channel : adChannels) {
            if (adChannel.getName().equals(channel.getName())){
                return ResponseResult.errorResult(AppHttpCodeEnum.NAME_SAME);
            }
        }

        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        if(adChannel.getDescription().length()>5000||adChannel.getName().length()>100){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOO_LONG);
        }
        if (adChannel==null||adChannel.getId()==null){
            return ResponseResult.errorResult((AppHttpCodeEnum.PARAM_INVALID));
        }
        //修改的数据是否存在,名字不相同则不存在
        AdChannel adChannel2 = getById(adChannel.getId());
        if (!adChannel2.getName().equals(adChannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //adChannel.setCreatedTime(new Date());
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id==null){
            return ResponseResult.errorResult((AppHttpCodeEnum.PARAM_INVALID));
        }
        AdChannel adChannel = getById(id);
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        }
       // int i=3/0;
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public List<AdChannel> findAll() {
        return list();
    }


}

